<?php

class SPage 
{
	private $spageid ='';
	private $name ='';
	private $body ='';
	
	function SPage()
	{
		
	}
	
	function setData($data) 
	{
		$this->spageid = $data['id'];
		$this->name= $data['name'];
		$this->body = $data['body'];
	}
	
	function getID() 
	{
		return $this->spageid;
	}
	
	function getName() 
	{
		return $this->name;
	}
	
	function getBody() 
	{
		return $this->body;
	}
	
	function displaySPage($spageid)
	{
		
		$spage = SPage::getSPage($spageid);
		
		//load post template
		$temp = new template();
		$temp->load($GLOBALS['prefs']['theme']."/post.php");
		
		//new
	
		//$temp->assign("post_link",WEBSITE_URL."index.php?action=spage&amp;id=".$this->getID());
		$temp->assign("post_title",$spage->name);
		$temp->assign("timestamp",date('F jS \of Y',strtotime($spage->date)));
		$temp->assign("text",$spage->body);		
		$temp->render();
	}
	
	function displayPageInList()
	{
		echo '
		<div class="comments-template">
			<ol class="commentlist">
	
				<li>
			
					<h3>
						<a href="'.WEBSITE_URL.'index.php?action=admin&amp;pages=edit&amp;spageid='.$this->spageid.'" title="Edit '.$this->name.'">'.$this->name.'</a>
					</h3> 
	
					<a href="'.WEBSITE_URL.'index.php?action=admin&amp;pages=edit&amp;spageid='.$this->spageid.'" title="Edit '.$this->name.'">(edit)</a> | <a href="'.WEBSITE_URL.'index.php?action=admin&amp;pages=delete&amp;spageid='.$this->spageid.'" title="Delete '.$this->name.'">(delete)</a>				
				</li>
				
			</ol>
		</div>	
		
		';
	}
	
	
	function getSPage($spageid)
	{
		//select comments, for this post id
		$query = "SELECT id,name,body FROM spage WHERE id=$spageid";
		
		$re = getAssoc($query);
		
		$temp = new SPage();
		$temp->setData($re);
		
		return $temp;
	}
	
	function getAll()
	{
		$pages = array();
		
		//select comments, for this post id
		$query = "SELECT id,name,body FROM spage ORDER BY name ASC";
		
		//rezultati iskanja
		$result = getFullAssoc($query);
		
		foreach ($result as $re)
		{
			$temp =new Spage();
			$temp->setData($re);
			$pages[] = $temp;
		}
		
		return $pages;
	}
	
	function updateSpage()
	{
		$spageid =  $_REQUEST['pageid'];
		$name =  $_REQUEST['name'];
		$body =  $_REQUEST['body'];
		
		//create sql query 
		$sql = "UPDATE spage SET name = '$name', body = '$body' WHERE spage.id = $spageid LIMIT 1;";  
		
		$result = query($sql);
		
		echo 'Page updated succesfully!';
	}
	
	function addSpage()
	{	
		$spageid =  $_REQUEST['pageid'];
		$name =  $_REQUEST['name'];
		$body =  $_REQUEST['body'];
		
		//create sql query 
		$sql ="INSERT INTO `spage` (`name`, `body`) VALUES ('$name', '$body')";  
		
		$result = query($sql);
		
		echo 'Page added succesfully!';
	}
	
	function deleteSpage()
	{
		$spageid =  $_REQUEST['spageid'];
		
		//create sql query 
		$sql = "DELETE FROM spage WHERE id = $spageid LIMIT 1;";  
		
		$result = query($sql);
		
		echo 'Page deleted succesfully!';
	}
}

?>